package com.itheima.listener;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;
/**
 * 延迟队列消费者端
 */
@Component
public class OderListener implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        // 0、获取消息的标签
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            // 1、接收转换消息
            System.out.println("1、接收消息:" + (new String(message.getBody())));

            // 2、处理业务逻辑
            System.out.println("2、处理业务逻辑...");
            System.out.println("根据订单ID查询状态...");
            System.out.println("判断状态是否为支付成功...");
            System.out.println("如果没有支付成功,取消订单,回滚库存...");

            // 3、手动签收
            /*basicAck(long deliveryTag, boolean multiple)
             * 1、deliveryTag：接收到消息的标签
             * 2、multiple：是否签收多条消息 true:多条 false:仅当前deliveryTag所对应的消息
             * */
            channel.basicAck(deliveryTag,true);
        }catch (Exception e){
            System.out.println("出现异常,拒绝签收...");
            // 4、出现异常,拒绝签收
            /*basicReject(long deliveryTag, boolean requeue)*/
            //      拒绝签收        消息标签      重回队列
            channel.basicReject(deliveryTag,true);
        }
    }
}
